package com.fkjslee.hos;

import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonAnyFormatVisitor;
import com.fkjslee.hos.entity.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fkjslee
 * @date 2017/12/28 at 15:49
 * Copyright (c) 2017 fkjslee. All rights reserved.
 */

@RestController
@Transactional
public class HosController {
    public static Integer maxPerson = 10;

    @Autowired
    private AccountRepository accountRepository;
    @Autowired
    private PatientInExpertRepository patientInExpertRepository;
    @Autowired
    private MedicineRepository medicineRepository;
    @Autowired
    private PrescriptionRepository prescriptionRepository;
    @Autowired
    private ExpertRepository expertRepository;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String say() {
        return "hello world!";
    }

    @RequestMapping(value = "/login")
    public String login(String account, String pwd) {
        Account user = accountRepository.findByAccount(account);
        PatientInExpert pine = patientInExpertRepository.findFirstByExpertId(user.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", user.getType());
        if(user == null) return "noThisOne";
        else if(user.getPwd().equals(pwd) || user.getType().equals("p")) return jsonObject.toString();
        else return "wrongPwd";
    }

    @RequestMapping(value = "/storePrescription", method=RequestMethod.GET)
    public String storePrescription(String patientID, String account,
                                  String medicineName1, String medicineNum1, String price1,
                                  String medicineName2, String medicineNum2, String price2,
                                  String medicineName3, String medicineNum3, String price3) {
        if(medicineName1 == null || medicineName1.equals("")) medicineName1 = "0";
        if(medicineName2 == null || medicineName2.equals("")) medicineName2 = "0";
        if(medicineName3 == null || medicineName3.equals("")) medicineName3 = "0";
        if(medicineNum1 == null || medicineNum1.equals("")) medicineNum1 = "0";
        if(medicineNum2 == null || medicineNum2.equals("")) medicineNum2 = "0";
        if(medicineNum3 == null || medicineNum3.equals("")) medicineNum3 = "0";
        if(price1 == null || price1.equals("")) price1 = "0";
        if(price2 == null || price2.equals("")) price2 = "0";
        if(price3 == null || price3.equals("")) price3 = "0";
        Prescription prescription = new Prescription();
        ArrayList<Medicine> arrayList = new ArrayList<>();
        arrayList.add(new Medicine(medicineRepository.findByName(medicineName1).getId(), Double.valueOf(price1), Integer.valueOf(medicineNum1)));
        arrayList.add(new Medicine(medicineRepository.findByName(medicineName2).getId(), Double.valueOf(price2), Integer.valueOf(medicineNum2)));
        arrayList.add(new Medicine(medicineRepository.findByName(medicineName3).getId(), Double.valueOf(price3), Integer.valueOf(medicineNum3)));
        prescription.setDetail("no details");
        prescription.setExpertID(accountRepository.findByAccount(account).getId());
        prescription.setPatientID(Long.valueOf(patientID));
        prescription.setMedicines(arrayList);
        prescriptionRepository.save(prescription);
        Account user = accountRepository.findByAccount(account);
        System.out.println("delete = " + user.getId() + " " + patientID);
        patientInExpertRepository.deleteByExpertIdAndPatientID(user.getId(), Long.valueOf(patientID));
        PatientInExpert pine = patientInExpertRepository.findFirstByExpertId(user.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", user.getType());
        jsonObject.put("patientID", patientID);
        jsonObject.put("account", account);
        return jsonObject.toString();
    }

    @RequestMapping(value = "/prePersonNum")
    public String prePersonNum(String patientID) {
        PatientInExpert expertAccount = patientInExpertRepository.findFirstByPatientID(Long.valueOf(patientID));
        if(expertAccount == null) return "0";
        List<PatientInExpert> patientInExperts = patientInExpertRepository.findByExpertId(expertAccount.getExpertId());
        Integer i = 0;
        Boolean find = false;
        for(PatientInExpert patientInExpert : patientInExperts) {
            if(patientID.equals(String.valueOf(patientInExpert.getPatientID()))) {
                find = true;
                break;
            }
            i++;
        }
        if(!find) return "-1";
        else return String.valueOf(i);
    }

    @RequestMapping(value = "/clickBtnAppoint")
    public String clickBtnAppoint(String patientID, String expertID) {
        List<PatientInExpert> patientInExperts = patientInExpertRepository.findByExpertId(Long.valueOf(expertID));
        if(patientInExperts.size() > maxPerson) return "full";
        PatientInExpert patientInExpert = new PatientInExpert();
        patientInExpert.setExpertId(Long.valueOf(expertID));
        patientInExpert.setPatientID(Long.valueOf(patientID));
        patientInExpertRepository.save(patientInExpert);
        return "ok";
    }

    @RequestMapping(value = "/getExpertMsg")
    public String getExpertMsg() {
        List<Expert> experts = expertRepository.findAll();
        JSONArray jsonArray = new JSONArray();
        for(Expert expert : experts) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("expertID", String.valueOf(expert.getId()));
            jsonObject.put("detail", String.valueOf(expert.getDetail()));
            List<PatientInExpert> patientInExperts = patientInExpertRepository.findByExpertId(expert.getId());
            jsonObject.put("remain", String.valueOf(maxPerson - patientInExperts.size()));
            jsonArray.add(jsonObject);
        }
        return jsonArray.toString();
    }

    @RequestMapping(value = "/appoint")
    public String appoint(String expertID, String patientID) {
        System.out.println("appoint: patientID = " + patientID + "  expertID = " + expertID);
        List<PatientInExpert> patientInExperts = patientInExpertRepository.findByExpertId(Long.valueOf(expertID));
        if(patientInExperts.size() > maxPerson) return "maxPerson";
        for(PatientInExpert patientInExpert : patientInExperts) {
            if(patientInExpert.getPatientID().equals(Long.valueOf(patientID))) {
                return "has already";
            }
        }
        PatientInExpert patientInExpert = new PatientInExpert();
        patientInExpert.setExpertId(Long.valueOf(expertID));
        patientInExpert.setPatientID(Long.valueOf(patientID));
        patientInExpertRepository.save(patientInExpert);
        return "succeed";
    }

    @RequestMapping(value = "/getOnePatient")
    public String getOnePatient(String account) {
        PatientInExpert patientInExpert = patientInExpertRepository.findFirstByExpertId(Long.valueOf(account));
        if(patientInExpert == null) return "-1";
        return patientInExpert.getPatientID().toString();
    }

    @RequestMapping(value = "/getOneDispensing")
    public String getOneDispensing(String account) {
        Prescription prescription = prescriptionRepository.findFirstBy();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("patientID", String.valueOf(prescription.getPatientID()));
        List<Medicine> medicines = prescription.getMedicines();
        for(int i = 0; i < medicines.size(); ++i) {
            jsonObject.put("medicineName" + String.valueOf(i+1), medicines.get(i).getId());
            jsonObject.put("medicineNum" + String.valueOf(i+1), medicines.get(i).getNumber());
            jsonObject.put("medicinePrice" + String.valueOf(i+1), medicines.get(i).getPrice());
        }
        return jsonObject.toString();
    }

    @RequestMapping(value = "/dispensing")
    public void dispensing(String account, String patientID) {
        patientInExpertRepository.deleteByExpertIdAndPatientID(Long.valueOf(account), Long.valueOf(patientID));
    }
}
